package de.hdu.myjobapp.view.component;

import org.apache.log4j.Logger;

import com.vaadin.ui.*;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.event.Action;
import com.vaadin.event.ShortcutAction;

import de.hdu.myjobapp.ctrl.AuthenticationController;
import de.hdu.myjobapp.model.User;

import com.vaadin.ui.Button;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

import de.hdu.myjobapp.view.MyJobApplication;

/**
 * Creates the Login Form objects and handles 
 * Keyboard interaction with the objects
 * @author mta
 *
 */
@SuppressWarnings("serial")
public class LoginHandler extends VerticalLayout implements Action.Handler {

	// The login "Form".
	private transient Logger log = Logger.getLogger(LoginHandler.class);
	private AuthenticationController auth = null;
	private final TextField userField = new TextField();
	private final TextField passwordField = new TextField();
	private final Button submit = new Button("Submit",
			new Button.ClickListener() {
				public void buttonClick(ClickEvent e) {
					login((String) userField.getValue(), (String) passwordField.getValue());
				}

			});
	private final Button logoutButton = new Button("Logout",
			new Button.ClickListener() {
				public void buttonClick(ClickEvent e) {
					logout();
				}
			});


	// Allow input with the enter button.
	private final Action ACTION_LOGIN = new ShortcutAction("Login ",
			ShortcutAction.KeyCode.ENTER, null);
	/**
	 * integrierte Login Funktion, da Innerhalb des ButtonClick Events keine Möglichkeiten
	 * gegeben sind den ActionHandler zu entfernen
	 * integrated login method, because there are no possibilities to remove an actionhandler 
	 * inside a Buttonclick event  
	 */
	private void login(String username, String password){
		auth = (AuthenticationController)MyJobApplication.get().getApplicationContext().getBean("authenticationController");
		User user = auth.login(username, password);
		userField.setValue("");
		passwordField.setValue("");
		
		if ( user != null) {
			/*
			 *  Sets the application user and sends out an event to inform that
			 *  the user has logged in. UiHandler will receive this event.
			 */
			MyJobApplication.getProject().setUser((Object) user);
			//user = (User)MyJobApplication.getProject().getUser();
			// Remove the action handler to the Enter key.
			MyJobApplication.getProject().getMainWindow().removeActionHandler(this);
		//	getApplication().getMainWindow().showNotification(" login " + user.getUserLogin() + "pass " + user.getUserPassword() + "Form " + password + username);
		} else {
			//getApplication().getMainWindow().showNotification(" null " + user.getUserLogin() + "pass " + user.getUserPassword()+ "Form " + password + username);
		}
	}
	/**
	 * Internal Logout method to change the Visuals and inform
	 * The application of the User Change
	 */
	private void logout(){
		userField.setValue("");
		passwordField.setValue("");
		MyJobApplication.getProject().setUser(null);
		// Add back the ActionHandler for the Enter key.
		MyJobApplication.getProject().getMainWindow().addActionHandler(this);
	}
	/**
	 * Handles the login box on the page. 
	 * @param SpringContextHelper Object 
	 */
	@SuppressWarnings("deprecation")
	public LoginHandler() {
//		SpringContextHelper helper = new SpringContextHelper(getApplication());
//		SpringContextHelper helper = null;
//		auth = (AuthenticationController)helper.getBean("authenticationController");
		
		// Style the login box.
		setMargin(true);
		setWidth("200");
		setHeight("100");

		// Hides the text input for the password text field.
		passwordField.setSecret(true);

		createLoginComponents();

		// Adds keyboard listener
		MyJobApplication.getProject().getMainWindow().addActionHandler(this);
	}


	/**
	 * Inserts the login-"Form" for the user.
	 */
	public void createLoginComponents() {
		
		// Style the login box.
		setMargin(true);
		setWidth("200");
		setHeight("100");

		// Hides the text input for the password text field.
		passwordField.setSecret(true);

		// Clears all current components in case the user already logged in.
		removeAllComponents();

		// Adds the "Form" for the login event.
		addComponent(userField);
		setComponentAlignment(userField, Alignment.TOP_RIGHT);

		addComponent(passwordField);
		setComponentAlignment(passwordField, Alignment.MIDDLE_RIGHT);

		addComponent(submit);
		setComponentAlignment(submit, Alignment.BOTTOM_RIGHT);

	}

	/**
	 * Creates the login information box and the logout button.
	 */
	public void createLogoutComponents() {

		// Style the login box.
		setMargin(true);
		setWidth("200");
		setHeight("100");

		// Hides the text input for the password text field.
		passwordField.setSecret(true);
		
		// User name
		Label welcome = new Label("Willkommen, ");
		User user = (User)MyJobApplication.getProject().getUser();
		Label person = new Label(user.getUserName());

		// Clears all current components.
		removeAllComponents();

		addComponent(welcome);
		setComponentAlignment(welcome, Alignment.TOP_RIGHT);
		addComponent(person);
		setComponentAlignment(person, Alignment.MIDDLE_RIGHT);
		addComponent(logoutButton);
		setComponentAlignment(logoutButton, Alignment.BOTTOM_RIGHT);

	}
	/**
	 * Get Keyboard Actions (e.g. ENTER)
	 */
	public Action[] getActions(Object target, Object sender) {
		return new Action[] { ACTION_LOGIN };
	}
	/**
	 * This method runs in Case of an Action Event 
	 */
	public void handleAction(Action action, Object sender, Object target) {
		// Handle only events that originate from Login window

		if (action == ACTION_LOGIN) {
			login((String) userField.getValue(), (String) passwordField.getValue());
		}

	}


}